package com.cn.test.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.filter.RequestContextFilter;


import com.sinopec.siam.agent.web.AccessEnforcer;
import com.sinopec.siam.agent.web.SAMLProfileFilter;

@Configuration
//@PropertySource("")
public class ApplicationConfiguration {
	
	
//	@Bean
//	public ServletListenerRegistrationBean contextLoadListenerRegistration() {
//		ServletListenerRegistrationBean  registration = new ServletListenerRegistrationBean ();
//		registration.setListener(new ContextLoaderListener());
//		return registration;
//	}
//	

	@Bean
	public ServletListenerRegistrationBean requestContextListenerRegistration() {
		ServletListenerRegistrationBean  registration = new ServletListenerRegistrationBean ();
		registration.setListener(new RequestContextListener());
		return registration;
	}
	
	
	@Bean
    public FilterRegistrationBean RequestContextFilterRegistration() {

        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new RequestContextFilter());
        registration.addUrlPatterns("/*");
//        registration.addInitParameter("paramName", "paramValue");
//        registration.setName("testFilter");
//        registration.setOrder(1);
        return registration;
    }
	@Bean
	public FilterRegistrationBean SAMLProfileFilterRegistration() {
		
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new SAMLProfileFilter());
		registration.addUrlPatterns("/SSO/*");
//        registration.addInitParameter("paramName", "paramValue");
//        registration.setName("testFilter");
		registration.setOrder(2);
		return registration;
	}
	@Bean
	public FilterRegistrationBean AccessEnforcer4ClientCertRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new AccessEnforcer());
		registration.addUrlPatterns("/main4cert.jsp");
        registration.addInitParameter("AuthenticationMethod", "urn:oasis:names:tc:SAML:2.0:ac:classes:TLSClient");
        registration.addInitParameter("AlwaysReauthen", "false");
        registration.setName("AccessEnforcer4ClientCert");
		registration.setOrder(5);
        System.out.println("AccessEnforcer4ClientCertRegistration");

		return registration;
	}

	@Bean
	public FilterRegistrationBean AccessEnforcer4OTPRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new AccessEnforcer());
		registration.addUrlPatterns("/main4otp.jsp");
        registration.addInitParameter("AuthenticationMethod", "urn:oasis:names:tc:SAML:2.0:ac:classes:OTPUsernamePassword");
        registration.addInitParameter("AlwaysReauthen", "false");
        registration.setName("AccessEnforcer4OTP");
		registration.setOrder(2);
        System.out.println("AccessEnforcer4OTPRegistration");

		return registration;
	}
	

	
	@Bean
	public FilterRegistrationBean AccessEnforcer4TamLdapRegistration() {
		FilterRegistrationBean registration = new FilterRegistrationBean();
		registration.setFilter(new AccessEnforcer());
		registration.addUrlPatterns("/main4tamldap");
		registration.addInitParameter("AuthenticationMethod", "urn:oasis:names:tc:SAML:2.0:ac:classes:TAMUsernamePassword");
		registration.addInitParameter("AlwaysReauthen", "false");
		registration.setName("AccessEnforcer4TamLdap");
		registration.setOrder(1);

		return registration;
	}
//	
//	@Bean
//	public FilterRegistrationBean test() {
//		FilterRegistrationBean registration = new FilterRegistrationBean();
//		registration.setFilter(new AccessEnforcer());
//		registration.addUrlPatterns("/main4tamldap.jsp");
//		registration.addInitParameter("AuthenticationMethod", "urn:oasis:names:tc:SAML:2.0:ac:classes:TAMUsernamePassword");
//		registration.addInitParameter("AlwaysReauthen", "false");
//		registration.setName("AccessEnforcer4TamLdap");
//		
//		
//		registration.setOrder(1);
//        System.out.println("AccessEnforcer4TamLdapRegistration");
//
//		return registration;
//	}
}
